//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController');
	//var
	com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController = function() {
		com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController.initializeBase(this);
		this.initialize();
	}
	var preview = "";
	var preInnum = 0;
	var pk;
	var isFromScanList = false;

	function com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$evaljs(js) {
		eval(js)
	}

	/**
	 * 初始化界面值
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function loadDetail(sender, args) {
		initScanedTable();
		preview = $jsonToString($param.getJSONObject("preview"));
		var tmp = {
			"scannum" : "1",
			"dbizdate" : NCMMPubUtil.getNowDate()
		};

		//从下载界面图1的头扫描进入
		var billinfo = $param.getJSONObject("billinfo");		
		if (billinfo != null) {
			billinfo = NCMMPubUtil.htmlspecialjson2UI(billinfo);			
			//$alert("billinfo="+$jsonToString(billinfo));
			$ctx.push(billinfo);
		}

		//从扫描记录的图4行修改进入
		var scanlistrow = $param.getJSONObject("scanlistrow");		
		if (scanlistrow != null) {
			scanlistrow = NCMMPubUtil.htmlspecialjson2UI(scanlistrow);
			pk = scanlistrow["ts"];
			isFromScanList = true;
			preInnum = scanlistrow["scannum"];
			//$alert("scanlistrow="+$jsonToString(scanlistrow));
			$ctx.push(scanlistrow);
		}

		//从下载界面图1的行采集进入
		var showlistrow = $param.getJSONObject("showlistrow");
		//showlistrow = NCMMPubUtil.htmlspecialjson2UI(showlistrow);
		if (showlistrow != null) {
			showlistrow = NCMMPubUtil.htmlspecialjson2UI(showlistrow);
			//带入下载下来的仓库
			showlistrow["cwarehouseid"]=showlistrow["pk_receivestore"];
			showlistrow["receivestorename"] = showlistrow["receivestorename"];//NCMMPUINUtil.getDefaultStordocName();
			//$alert("showlistrow="+$jsonToString("showlistrow"));
			$ctx.push(showlistrow);
		}
	}

	function fillctxByScanListvo(vo) {
		$ctx.put("pkarriveorderb", scanlistrow["csourcebillbid"]);
		$ctx.put("pk_arriveorder", scanlistrow["csourcebillhid"]);
		$ctx.put("bts", scanlistrow["csourcebodyts"]);
		$ctx.put("hts", scanlistrow["csourceheadts"]);
		$ctx.dataBind();
	}

	/**
	 * 判断是否需要创建到货单扫描记录表
	 */
	function initScanedTable() {
		if ($cache.read("isCreatePuInScaneTable") == "true")
			return;
		NCMMPubUtil.dropTable(PurchaseInScanedVO);
		NCMMPubUtil.createTable(PurchaseInScanedVO);
		$cache.write("isCreatePuInScaneTable", "true");
	}

	/**
	 * 显示更多菜单项
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function showMenuList(sender, args) {
		NCMMPUINUtil.showMenuLists("imagebutton4");
	}

	/**
	 * 根据扫入的物料码获取物料信息
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function scanMaterialcode(sender, args) {

		$ctx.dataCollect();
		var vbillcode = $ctx.getString("vbillcode");
		//1首先肯定单据码是存在的，确定单据号
		if (vbillcode == null || vbillcode == "") {
			$alert("请先扫单据码或者请选择单据");
			return;
		}
		//1 暂存记录
		//if (NCMMPUINUtil.getDefaultStordocPK() == null || NCMMPUINUtil.getDefaultStordocPK() == "") {
		//	$alert("请先在业务设置中设置仓库");
		//	return;
		//}

		$scanner.open({
			bindfield : "vcode",
			callback : "marScanBack()"
		});
		 $id("textbox3").set("value", null);
	}

	function marScanBack() {
		var marbarcode = $ctx.getString("vcode");
		if (marbarcode != null && marbarcode != "") {
		 // 扫描的是物料码
		 $id("textbox3").set("value", marbarcode);
		 $ctx.dataBind();
		 }
		analysisMtrlInfoShow(marbarcode);
		$id("textbox3").set("value", null);
		//$(textbox0)
	}
	function clearMaterialInfo() {
		//物料vid
		$ctx.put("pk_material", "");
		//批次号
		$ctx.put("vbatchcode", "");
		$ctx.put("pk_batchcode", "");
		//序列号
		$ctx.put("vserialcode", "");
		$ctx.dataBind();
	}

	function getMaxInVo(vos) {
		var rtnvo = vos[0];
		var max = NCMMPUINUtil.getSubScanedNum(rtnvo["nnum"], rtnvo["scanednum"]);
		for (var i = 1; i < vos.length; i++) {
			var tmpnum = NCMMPUINUtil.getSubScanedNum(vos[i]["nnum"], vos[i]["scanednum"]);
			if (NCMMPUINUtil.isBigger(tmpnum, max)) {
				max = tmpnum;
				rtnvo = vos[i];
			}
		}
		return rtnvo;
	}

	/**
	 * 用扫描的物料码查询物料信息显示在编辑界面
	 */
	function analysisMtrlInfoShow(marbarcode) {
	   
		//var marbarcode = $ctx.getString("vcode");
		//if (marbarcode != null && marbarcode != "") {
		 // 扫描的是物料码
		 //	$id("textbox3").set("value", marbarcode);
		 //	$ctx.dataBind();
		 //}
		 if (NCMMPubUtil.isEmpty(marbarcode)) {
			$alert("物料码不能为空！");
			return;
		}
		var marJson = NCMMBarcodeParseUtil.parseMaterialBar(marbarcode);
		var marID = marJson.pk_material;
		if (NCMMPubUtil.isEmpty(marID)) {
			$alert("解析物料码失败，请检查输入是否正确，或更新物料档案！");
			return;
		}
		//1 获取单据码，然后解析出单据号信息，从本地数据库中查找该码对应的单据信息信息赋值
		var wheresql = " where pk_material='" + marID + "' and vbillcode ='" + $ctx.getString("vbillcode") + "'";
		NCMMPubUtil.queryVO(PurchaseInVO, wheresql, function(data) {
			//2 打开扫描记录编辑界面
			if (NCMMPubUtil.isEmpty(data)) {
				$alert("扫描物料" + marJson.code + "不在对应的单据" +$ctx.getString("vbillcode") + "中");
				return;
			}
			var vos = eval(data);
			vos=NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			var insertvo;
			//扫描的批次号为空
			if (NCMMPubUtil.isEmpty(marJson.vbatchcode)) {
				if (!NCMMPubUtil.isEmpty(vos) && vos.length == 1) {
					insertvo = vos[0];
				} else if (vos != null && vos.length >= 1) {
					insertvo = getMaxInVo(vos);

				} else {

					$alert("扫描物料编码：" + marJson.code + "不在单据" + $ctx.getString("vbillcode") + "中");
					return;
				}
			}
			//批次号严格匹配
			else {
				if (!NCMMPubUtil.isEmpty(vos) && vos.length == 1) {

					if (marJson.vbatchcode == vos[0]["vbatchcode"] || NCMMPubUtil.isEmpty(vos[0]["vbatchcode"])) {
						insertvo = vos[0];
					} else {
						$alert("单据批次号与物料批次号不同，解析失败");
						return;
					}
				} else if (vos != null && vos.length >= 1) {
					var batch = new Array();
					var batchEmpty = new Array();
					for (var i = 0; i < vos.length; i++) {
						if (marJson.vbatchcode == vos[i]["vbatchcode"]) {
							batch.push(vos[i]);
						} else if (NCMMPubUtil.isEmpty(vos[i]["vbatchcode"])) {
							batchEmpty.push(vos[i]);
						}
					}
					if (!NCMMPubUtil.isEmpty(batch)) {
						insertvo = getMaxInVo(batch);
					} else {
						if (!NCMMPubUtil.isEmpty(batchEmpty)) {
							insertvo = getMaxInVo(batchEmpty);
						} else {
							$alert("单据批次号与物料批次号不同，解析失败");
							return;
						}
					}

				} else {
					$alert("扫描物料编码：" + marJson.code + "不在单据" + $ctx.getString("vbillcode") + "中");
					return;

				}
			}
			isFillVbatchcodeByMarBar=NCMMPubUtil.isNotEmpty(marJson.vbatchcode);
			fillInfoByMarBar(marJson,isFillVbatchcodeByMarBar);
			//考虑批次号提示
			// $alert("待插入的insertvo="+$jsonToString({insertvo:insertvo}));
			fillScanedVoByPuinVO(insertvo,isFillVbatchcodeByMarBar,marJson);
			var ctxvo = getScanedVO();
			savevo(ctxvo);
			updatePuInVOAddScanedNum(ctxvo["csourcebillbid"],ctxvo["scannum"]);
			preInnum = ctxvo["scannum"];
		});
	}

	function updatePuInVOAddScanedNum(rowid,scanNum) {
		var condition = {
			"pkarriveorderb" : rowid
		};
		//更新到货单入库列表增加扫描数量 清空界面数据
		NCMMPubUtil.queryVOByConditions(PurchaseInVO, condition, function(data) {
			if (NCMMPubUtil.isEmpty(data)) {
				return;
			}
			vos = eval(data);
			vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			// $alert("更新前vos[0]="+$jsonToString(vos[0]));
			var newScanedNum = 0;
			if (vos[0]["scanednum"] == null || vos[0]["scanednum"] == "") {
				vos[0]["scanednum"] += "0";
			} else {
				newScanedNum = parseFloat(vos[0]["scanednum"]);
			}
			newScanedNum = parseFloat(scanNum).add(newScanedNum);
			if (newScanedNum < 0) {
				newScanedNum = 0;
			}
			vos[0]["scanednum"] = getNumByBit(newScanedNum, vos[0]["cunitid"]);
			//3.2 更新到货单表已扫描主数量
			// $alert("更新到货单表已扫描主数量vos[0]="+$jsonToString(vos[0]));
			NCMMPubUtil.updateVO(PurchaseInVO, vos[0]);

		});
	}

	function fillInfoByMarBar(marJson,isFillVbatchcodeByMarBar) {
		//物料vid
		$ctx.put("pk_material", marJson.pk_material);
		$ctx.put("pksrcmaterialcode", marJson.code);
		if(isFillVbatchcodeByMarBar){
			//批次号		
			$ctx.put("vbatchcode", marJson.vbatchcode);
			$ctx.put("pk_batchcode", marJson.pk_batch);
		}
		
		//序列号
		$ctx.put("vserialcode", marJson.vserialcode);
		$ctx.put("vcode", "");
	}

	function getCtxVO() {
		var vo = {
			//表体主键
			"csourcebillbid" : $ctx.getString("csourcebillbid"),
			//表头主键
			"csourcebillhid" : $ctx.getString("csourcebillhid"),
			//表头时间戳
			"csourceheadts" : $ctx.getString("csourceheadts"),
			//物料vid
			"cmaterialvid" : $ctx.getString("cmaterialvid"),
			//表体时间戳
			"csourcebodyts" : $ctx.getString("csourcebodyts"),
			//主单位
			"cunitid" : $ctx.getString("cunitid"),
			//主单位名称
			"cunitidname" : $ctx.getString("cunitidname"),
			//单位
			"castunitid" : $ctx.getString("castunitid"),
			//单位名称
			"castunitidname" : $ctx.getString("castunitidname"),
			//换算率
			"vchangerate" : $ctx.getString("vchangerate"),
			//供应商
			"cvendorid" : $ctx.getString("cvendorid"),
			//货位
			"pk_rack" : $ctx.getString("pk_rack"),
			//单据号
			"vbillcode" : $ctx.getString("vbillcode"),
			//物料编码
			"pksrcmaterialcode" : $ctx.getString("pksrcmaterialcode"),
			//物料名称
			"materialname" : $ctx.getString("materialname"),
			//规格
			"materialspec" : $ctx.getString("materialspec"),
			//型号
			"materialtype" : $ctx.getString("materialtype"),
			//行号
			"crowno" : $ctx.getString("crowno"),
			//仓库
			"receivestorename" : $ctx.getString("receivestorename"),
			//货位
			"pk_rackname" : $ctx.getString("pk_rackname"),
			//仓库
			"cwarehouseid" : $ctx.getString("cwarehouseid"),//NCMMPUINUtil.getDefaultStordocPK(),
			//供应商
			"pk_suppliername" : $ctx.getString("pk_suppliername"),
			//供应商批次号
			"vvendbatchcode" : $ctx.getString("vvendbatchcode"),
			//批次号
			"vbatchcode" : $ctx.getString("vbatchcode"),
			"pk_batchcode" : $ctx.getString("pk_batchcode"),
			//序列号
			"vserialcode" : $ctx.getString("vserialcode"),
			//入库日期
			"dbizdate" : $ctx.getString("dbizdate"),

			//主数量
			"nnum" : $ctx.getString("nnum"),
			//数量
			"nastnum" : $ctx.getString("nastnum"),
			//实收主数量
			"scannum" : $ctx.getString("scannum") == "" ? getNumByBit(1, $ctx.getString("cunitid")) : getNumByBit($ctx.getString("scannum"), $ctx.getString("cunitid")),
			//实收数量
			"scanastnum" : $ctx.getString("scanastnum") == "" ? getNumByBit(1, $ctx.getString("castunitid")) : getNumByBit($ctx.getString("scanastnum"), $ctx.getString("castunitid"))
		};
		return vo;
	}

	function getNumByBit(num, cunitid) {
		var rtn;
		if (!NCMMPubUtil.isEmpty(cunitid)) {
			rtn = NCMMPUINUtil.calNumByBitnumber(num, cunitid);
			if (rtn["flag"]) {
				num = rtn["num"];
			}
		}
		return num;
	}

	function getScanedVO() {
		var vo = {
			//表体主键
			"csourcebillbid" : $ctx.getString("pkarriveorderb"),
			//表头主键
			"csourcebillhid" : $ctx.getString("pk_arriveorder"),
			//表头时间戳
			"csourceheadts" : $ctx.getString("hts"),
			//物料vid
			"cmaterialvid" : $ctx.getString("pk_material"),
			//表体时间戳
			"csourcebodyts" : $ctx.getString("bts"),
			//主单位
			"cunitid" : $ctx.getString("cunitid"),
			//主单位名称
			"cunitidname" : $ctx.getString("cunitidname"),
			//单位
			"castunitid" : $ctx.getString("castunitid"),
			//单位名称
			"castunitidname" : $ctx.getString("castunitidname"),
			//换算率
			"vchangerate" : $ctx.getString("vchangerate"),
			//供应商
			"cvendorid" : $ctx.getString("pk_supplier"),
			//货位
			"pk_rack" : $ctx.getString("pk_rack"),
			//单据号
			"vbillcode" : $ctx.getString("vbillcode"),
			//物料编码
			"pksrcmaterialcode" : $ctx.getString("pksrcmaterialcode"),
			//物料名称
			"materialname" : $ctx.getString("materialname"),
			//规格
			"materialspec" : $ctx.getString("materialspec"),
			//型号
			"materialtype" : $ctx.getString("materialtype"),
			//行号
			"crowno" : $ctx.getString("crowno"),
			//仓库
			"receivestorename" : $ctx.getString("receivestorename"),
			//货位
			"pk_rackname" : $ctx.getString("pk_rackname"),
			//仓库 默认仓库为空取单据上的仓库
			"cwarehouseid" : NCMMPUINUtil.getDefaultStordocPK()||$ctx.getString("pk_receivestore"),
			//供应商
			"pk_suppliername" : $ctx.getString("pk_suppliername"),
			//供应商批次号
			"vvendbatchcode" : $ctx.getString("vvendbatchcode"),
			//批次号
			"vbatchcode" : $ctx.getString("vbatchcode"),
			"pk_batchcode" : $ctx.getString("pk_batchcode"),
			//序列号
			"vserialcode" : $ctx.getString("vserialcode"),
			//入库日期
			"dbizdate" : $ctx.getString("dbizdate"),

			//数量
			"nnum" : $ctx.getString("nnum"),
			//主数量
			"nastnum" : $ctx.getString("nastnum"),
			//实收主数量
			"scannum" : $ctx.getString("scannum") == "" ? getNumByBit(1, $ctx.getString("cunitid")) : getNumByBit($ctx.getString("scannum"), $ctx.getString("cunitid")),
			//实收数量
			"scanastnum" : $ctx.getString("scanastnum") == "" ? getNumByBit(1, $ctx.getString("castunitid")) : getNumByBit($ctx.getString("scanastnum"), $ctx.getString("castunitid"))
		};
		return vo;
	}

	/**
	 * 从本地数据库中读取表体信息显示在编辑界面
	 * @param {Object} vo
	 */
	function fillScanedVoByPuinVO(vo,isFillVbatchcodeByMarBar,marJson) {
	    var num=1;
	    var vchangerate=vo["vchangerate"];
	    var nastnum=NCMMPUINUtil.getNastum(num,vchangerate);
	    if(NCMMPubUtil.isNotEmpty(marJson.nnum)){
		  num=marJson.nnum;
		  nastnum = NCMMPUINUtil.getNastum(num, vchangerate);
		}
		else if(NCMMPubUtil.isNotEmpty(marJson.nassistnum)){
		  nastnum=marJson.nassistnum;
		  num = NCMMPUINUtil.getNum(nastnum, vchangerate);
		}
		//物料编码
		$ctx.put("pksrcmaterialcode", vo["pksrcmaterialcode"]);
		//物料名称
		$ctx.put("materialname", vo["materialname"]);
		//规格
		$ctx.put("materialspec", vo["materialspec"]);
		//型号
		$ctx.put("materialtype", vo["materialtype"]);
		//行号
		$ctx.put("crowno", vo["crowno"]);
		//仓库
		$ctx.put("receivestorename", vo["receivestorename"]);
		//货位
		$ctx.put("pk_rackname", vo["pk_rackname"]);
		//供应商
		$ctx.put("pk_suppliername", vo["pk_suppliername"]);
		//表体主键   后续提交暂存
		$ctx.put("pkarriveorderb", vo["pkarriveorderb"]);
		//表头主键  后续提交暂存
		$ctx.put("pk_arriveorder", vo["pk_arriveorder"]);
		//表头时间戳  后续提交暂存
		$ctx.put("hts", vo["hts"]);
		//物料vid 后续提交暂存
		$ctx.put("pk_material", vo["pk_material"]);
		//表体时间戳  后续提交暂存
		$ctx.put("bts", vo["bts"]);
		//主单位  后续提交暂存
		$ctx.put("cunitid", vo["cunitid"]);
		//主单位名称
		$ctx.put("cunitidname", vo["cunitidname"]);
		//单位
		$ctx.put("castunitid", vo["castunitid"]);
		//单位名称
		$ctx.put("castunitidname", vo["castunitidname"]);
		//换算率
		$ctx.put("vchangerate", vo["vchangerate"]);
		//供应商  后续提交暂存
		$ctx.put("pk_supplier", vo["pk_supplier"]);
		//货位  后续提交暂存
		$ctx.put("pk_rack", vo["pk_rack"]);
		if(!isFillVbatchcodeByMarBar){
			$ctx.put("vbatchcode", vo["vbatchcode"]);
			$ctx.put("pk_batchcode", vo["marJson.pk_batch"]);
		}
		//仓库
		$ctx.put("pk_receivestore", vo["pk_receivestore"]);
		$ctx.put("cwarehouseid", vo["pk_receivestore"]);
		//主数量
		$ctx.put("nnum", vo["nnum"]);
		//数量
		$ctx.put("nastnum", vo["nastnum"]);
		//扫描主数量
		$ctx.put("scannum", getNumByBit(num, vo["cunitid"]));
		// 设置默认的扫描主数量
		$ctx.put("scanastnum", getNumByBit(nastnum, vo["castunitid"]));
		$ctx.dataBind();
	}

	function isEmpty() {
		if (($ctx.getString("pkarriveorderb") != "" && $ctx.getString("pk_arriveorder") != "") || $ctx.getString("csourcebillbid") != "" && $ctx.getString("csourcebillhid") != "") {
			return false;
		} else {
			return true;
		}
	}

	function savevo(vo) {
		var date = new Date().getTime();
		date = {
			"ts" : date.toString()
		};
		pk = date["ts"];
		vo = $.extend({}, vo, date);
		vo = NCMMPubUtil.htmlspecialjson2UI(vo);
		if (NCMMPubUtil.isEmpty(vo["scannum"])) {
			vo["scannum"] = 1;
			var nastnum = NCMMPUINUtil.getNastum(1, vo["vchangerate"]);
			vo["scanastnum"] = nastnum;
		}
        //仓库 默认仓库为空取单据上的仓库
		vo["cwarehouseid"]=  NCMMPUINUtil.getDefaultStordocPK()||vo["cwarehouseid"],
		NCMMPubUtil.insertVO(PurchaseInScanedVO, vo);
	}

	function insertScanVO(vo) {
		savevo(vo);
		updatePuinVos(vo["csourcebillbid"]);

	}

	function queryPuinScanVOByPK() {
		var condition = {
			"ts" : pk
		};
		//更新到货单入库列表增加扫描数量 清空界面数据
		NCMMPubUtil.queryVOByConditions(PurchaseInScanedVO, condition, function(data) {
			if (NCMMPubUtil.isEmpty(data)) {
				return null;

			} else {
				return data;
			}
		});
	}

	function getScanedNum(voScanedNum) {
		var tmp = $ctx.getString("scannum");
		var scannum = $ctx.getString("scannum");

		scannum = NCMMPUINUtil.getSubNum(scannum, preInnum);

		preInnum = tmp;
		var rtnnum;
		if (scannum < 0) {
			if (voScanedNum == null || voScanedNum == "") {
				voScanedNum += "00.00000";
			} else {
				voScanedNum = parseFloat(voScanedNum);
			}
			rtnnum = parseFloat(voScanedNum) + parseFloat(scannum);
		} else {

			rtnnum = NCMMPUINUtil.getAddNum(voScanedNum, scannum);

		}
		return rtnnum;
	}

	function updatePuinVos(rowid) {
		// 更新到货单表的已扫描数量,用行id
		var condition = {
			"pkarriveorderb" : rowid
		};
		//更新到货单入库列表增加扫描数量 清空界面数据
		NCMMPubUtil.queryVOByConditions(PurchaseInVO, condition, function(data) {
			if (NCMMPubUtil.isEmpty(data)) {
				return;
			}
			vos = eval(data);
			vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			var newScanedNum = getScanedNum(vos[0]["scanednum"]);
			//$alert("newScanedNum="+newScanedNum.toString());
			vos[0]["scanednum"] = getNumByBit(newScanedNum, vos[0]["cunitid"]);
			//$alert("vos[0][scanednum]="+vos[0]["scanednum"].toString());
			//3.2 更新到货单表已扫描主数量
			NCMMPubUtil.updateVO(PurchaseInVO, vos[0]);

		});
	}

	function saveToScanedTable(sender, args) {

		//1 收集数据
		$ctx.dataCollect();

		//if (NCMMPUINUtil.getDefaultStordocPK() == null || NCMMPUINUtil.getDefaultStordocPK() == "") {
		//	$alert("请先在业务设置中设置仓库");
		//	return;
		//}
		if (NCMMPubUtil.isEmpty($ctx.getString("scannum")) || NCMMPubUtil.isEmpty($ctx.getString("scanastnum"))) {
			$alert("数量或主数量不能为空");
			return;
		}

		if (NCMMPubUtil.isEmpty($ctx.getString("vbillcode"))) {
			$alert("请先扫单据码");
			return;
		}
		if (NCMMPubUtil.isEmpty($ctx.getString("materialname"))) {
			$alert("请先扫物料码");
			return;
		}
		var vo;
		if (isFromScanList) {
			vo = getCtxVO();
		} else {
			vo = getScanedVO();
		}
		var id = {};
		if (pk == null || pk == "") {
			insertScanVO(vo);
			$toast("保存成功！");
			return;
		}
		id["ts"] = pk;
		vo = $.extend({}, vo, id);
		NCMMPubUtil.htmlspecialjson2UI(vo);
		var isvoexist = NCMMPubUtil.isScanVOExistByPK(PurchaseInScanedVO, pk)
		if (!isvoexist) {
			$alert("当前记录在扫描记录中已经被删除，请重新采集！");
			return;
		}
		//更新vo
		NCMMPubUtil.updateVO(PurchaseInScanedVO, vo);
		updatePuinVos(vo["csourcebillbid"]);
		$toast("保存成功");

	}

	function clearCtxInfo() {
		//单据号
		$ctx.put("vbillcode", "");
		clearSubCtxInfo();

	}

	/**
	 *提交将内容提交到nc系统中
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function submitPuIn(sender, args) {
	//默认仓库为空取单据上的仓库
	var stordoc=NCMMPUINUtil.getDefaultStordocPK()||$ctx.getString("cwarehouseid");
		if (stordoc == null || stordoc == "") {
			$alert("请先在业务设置中设置仓库");
			return;
		}
		if (NCMMPubUtil.isEmpty($ctx.getString("vbillcode"))) {
			$alert("请先扫单据码");
			return;
		}
		if (NCMMPubUtil.isEmpty($ctx.getString("materialname"))) {
			$alert("请先扫物料码");
			return;
		}
		if (NCMMPubUtil.isEmpty($ctx.getString("scannum")) || NCMMPubUtil.isEmpty($ctx.getString("scanastnum"))) {
			$alert("数量或主数量不能为空");
			return;
		}
		$js.showLoadingBar({
			msg : "正在提交数据，请稍后..."
		});
		var vo;
		if (isFromScanList) {
			vo = getCtxVO();
		} else {
			vo = getScanedVO();
		}

		if (NCMMPubUtil.isEmpty(pk)) {
			insertScanVO(vo);
		}
		if (NCMMPubUtil.isEmpty(vo["ts"])) {
			var id = {};
			id["ts"] = pk;
			vo = $.extend({}, vo, id);
		}
		//1 拼凑billinfos参数
		var billinfos = fillBillinfoArgs(vo);
		NCMMDBUtil.testNCConnection(function(data) {
			if (data == false || data == "false") {
				$js.hideLoadingBar();
				$toast("连接服务器失败，无法提交！");
				return;
			} else {
				//2 调用方法上传数据
				NCMMDBUtil.submitLocaldataToNC("23_45save", billinfos, function(data) {
					if (!NCMMPubUtil.isEmpty(data)) {
						if (data.return == null || data.return == "") {
							$js.hideLoadingBar();
							$alert($jsonToString(data));
							return;
						} else if (data.return.returnFlag == 1) {
							//更新到货单入库列表增加扫描数量 清空界面数据
							var condition = {
								"pkarriveorderb" : vo["csourcebillbid"]
							};
							//更新到货单入库列表增加扫描数量 清空界面数据
							NCMMPubUtil.queryVOByConditions(PurchaseInVO, condition, function(data) {
								var vos = eval(data);
								var newScanedNum = getScanedNum(vos[0]["scanednum"]);
								//如果是提交需要判断是否扫描主数量等于应收主数量，相等时删除此到货单
								vos[0]["scanednum"] = getNumByBit(NCMMPUINUtil.getSubNum(newScanedNum, $ctx.getString("scannum")), $ctx.getString("cunitid"));
                                vos[0]["nnum"] = getNumByBit(NCMMPUINUtil.getSubNum(vos[0]["nnum"], $ctx.getString("scannum")), $ctx.getString("cunitid"));
								var nastnum = NCMMPUINUtil.getNastum(vos[0]["nnum"], vos[0]["vchangerate"]);
								vos[0]["nastnum"] = getNumByBit(nastnum, $ctx.getString("castunitid"));
								if (vos[0]["nnum"] <= 0 || vos[0]["nastnum"] <= 0) {
									NCMMPubUtil.deleteVO(PurchaseInScanedVO, vo);
									NCMMPubUtil.deleteVO(PurchaseInVO, vos[0]);
									//5 清楚数据信息
									clearCtxInfo();
									$js.hideLoadingBar();
									//显示提交成功，
									$toast("提交成功");
									return;
								}
								//3.2 更新到货单表已扫描主数量
								NCMMPubUtil.updateVO(PurchaseInVO, vos[0]);
								//删除本地扫描数据
								NCMMPubUtil.deleteVO(PurchaseInScanedVO, vo);
								//5 清楚数据信息
								clearCtxInfo();
								$js.hideLoadingBar();
								//显示提交成功，
								$toast("提交成功");
							});
						} else {
							$js.hideLoadingBar();
							$alert("上传失败" + $jsonToString(data.return.returnDesc));
						}
					} else {
						$js.hideLoadingBar();
						$alert("上传失败");
					}
				});
			}
		});
	}

	function clearSubCtxInfo() {
		//物料编码
		$ctx.put("pksrcmaterialcode", "");
		//物料名称
		$ctx.put("materialname", "");
		//行号
		$ctx.put("crowno", "");
		//仓库
		$ctx.put("receivestorename", "");
		$ctx.put("pk_receivestore", "");
		$ctx.put("cwarehouseid", "");
		//货位
		$ctx.put("pk_rackname", "");
		//供应商
		$ctx.put("pk_suppliername", "");
		//供应商批次号
		$ctx.put("vvendbatchcode", "");
		//批次号
		$ctx.put("vbatchcode", "");
		//序列号
		$ctx.put("vserialcode", "");
		//表体主键   后续提交暂存
		$ctx.put("pkarriveorderb", "");
		//表头主键  后续提交暂存
		$ctx.put("pk_arriveorder", "");
		//表头时间戳  后续提交暂存
		$ctx.put("hts", "");
		//物料vid 后续提交暂存
		$ctx.put("pk_material", "");
		//表体时间戳  后续提交暂存
		$ctx.put("bts", "");
		//主单位  后续提交暂存
		$ctx.put("cunitid", "");
		//供应商  后续提交暂存
		$ctx.put("pk_supplier", "");
		//货位  后续提交暂存
		$ctx.put("pk_rack", "");
		//可入库主数量  后续提交暂存
		$ctx.put("nnum", "");
		//可入库主数量  后续提交暂存
		$ctx.put("nastnum", "");
		//规格
		$ctx.put("materialspec", "");
		//规格
		$ctx.put("materialtype", "");
		//可入库主数量  后续提交暂存
		$ctx.put("scannum", "");
		$ctx.put("scanastnum", "");
		preInnum = 0;
		$ctx.dataBind();

	}

	function getItem(vo) {
		var item = {
			//表体主键
			"csourcebillbid" : vo["csourcebillbid"],
			//物料vid
			"cmaterialvid" : vo["cmaterialvid"],
			//表体时间戳
			"csourcebodyts" : vo["csourcebodyts"],
			//批次号
			"vbatchcode" : vo["vbatchcode"],
			"pk_batchcode" : vo["pk_batchcode"],
			//供应商批次
			"vvendbatchcode" : vo["vvendbatchcode"],
			//主单位
			"cunitid" : vo["cunitid"],
			//应收主数量
			"ninnum" : vo["scannum"],
			"nassistnum" : vo["scanastnum"]

		};
		if (!NCMMPubUtil.isEmpty($ctx.getString("vserialcode"))||!NCMMPubUtil.isEmpty($ctx.getString("pk_rack"))) {
			var bbitem = {
				"vserialcode" : $ctx.getString("vserialcode"),
				"nassistnum" : vo["scanastnum"],
				//应收主数量
				"nnum" : vo["scannum"],
				"clocationid" : vo["pk_rack"]
			}
			var bbitems = [{
				"bbitem" : bbitem
			}];
			item["bbitems"] = bbitems;
		}

		return item;
	}

	function getBbitems() {
		var bbitems = [{
			"vserialcode" : $ctx.getString("vserialcode")
		}];
		return bbitems;
	}

	function getHead(vo) {
	    //默认仓库为空取单据上的仓库
	    var stordocid=NCMMPUINUtil.getDefaultStordocPK();
	    if(NCMMPubUtil.isEmpty(stordocid)){
	      stordocid=vo["cwarehouseid"];
	    }	    
		var head = {
			//表头主键
			"csourcebillhid" : vo["csourcebillhid"],
			//表头时间戳
			"csourceheadts" : vo["csourceheadts"],
			//仓库 
			"cwarehouseid" : stordocid

		}
		return head;
	}

	/**
	 * 拼凑billinfos
	 */

	function fillBillinfoArgs(vo) {
		var items = [{
			"item" : getItem(vo)
		}];
		var billinfo = {
			"head" : getHead(vo),
			"items" : items
		};
		var billinfos = [{
			"billinfo" : billinfo
		}];
		return billinfos;
	}

	/**
	 * 返回
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function back(sender, args) {
		$view.close();
	}

	function calculateNumber(args, direction) {

	}

	/**
	 * 主数量变化时计算辅数量
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function calNum(sender, args) {
		var inputNumber = args["newvalue"];
		if (NCMMPubUtil.isEmpty(inputNumber)) {
			//计算主数量
			$ctx.put("scannum", "");

			$ctx.dataBind();
			return;
		}
		if (inputNumber == "-") {
			return;
		}
		$ctx.dataCollect();
		if (!$.isNumeric(inputNumber) || !NCMMPUINUtil.isBitEn($ctx.getString("scanastnum"), $ctx.getString("castunitid"))) {
			//辅数量计算主数量 辅数量为原来值 主数量不变
			if(NCMMPubUtil.isNotEmpty($ctx.getString("scanastnum"))){
				$ctx.put("scanastnum", args["oldvalue"]);
				$ctx.dataBind();
				return;
			}
		}
		var scanastnum = $ctx.getString("scanastnum");
		if (!$.isNumeric(scanastnum)) {
			if(NCMMPubUtil.isEmpty(scanastnum)){
				$ctx.put("scannum", "");
				$ctx.dataBind();
				return;
			}		
			$alert("输入不合法！");
			return;
		}
		if (!NCMMPubUtil.isEmpty(scanastnum)) {
			var vchange = $ctx.getString("vchangerate");
			calnum = NCMMPUINUtil.getNum(scanastnum, vchange);
			if (!NCMMPubUtil.isEmpty(calnum) && calnum.toString() == "NaN") {
				$alert("请输入合法数值");
				return;
			}
			calnum = getNumByBit(calnum, $ctx.getString("cunitid"));
		}
		$ctx.put("scannum", calnum.toString());
		$ctx.dataBind();
	}

	/**
	 * 主数量变化时计算辅数量
	 * @param {Object} sender
	 * @param {Object} args
	 */
	function calAstNum(sender, args) {
		var inputNumber = args["newvalue"];
		if (NCMMPubUtil.isEmpty(inputNumber)) {
			//计算辅数量
			$ctx.put("scanastnum", "");
			$ctx.dataBind();
			return;
		}
		if (inputNumber == "-") {
			return;
		}
		$ctx.dataCollect();
		if (!$.isNumeric(inputNumber) || !NCMMPUINUtil.isBitEn($ctx.getString("scannum"), $ctx.getString("cunitid"))) {
			//计算辅数量
			if(NCMMPubUtil.isNotEmpty($ctx.getString("scannum"))){		
				$ctx.put("scannum", args["oldvalue"]);
				$ctx.dataBind();
				return;
			}
		}
		var nnum = $ctx.getString("scannum");
		if (!$.isNumeric(nnum)) {
			if(NCMMPubUtil.isEmpty(nnum)){
				$ctx.put("scanastnum", "");
				$ctx.dataBind();
				return;
			}
			$alert("输入不合法！");
			return;
		}
		var calnum;
		if (!NCMMPubUtil.isEmpty(nnum)) {
			var vchange = $ctx.getString("vchangerate");
			calnum = NCMMPUINUtil.getNastum(nnum, vchange);
			if (!NCMMPubUtil.isEmpty(calnum) && calnum.toString() == "NaN") {
				$alert("请输入合法数值");
				return;
			}
			calnum = getNumByBit(calnum, $ctx.getString("castunitid"));
		}
		$ctx.put("scanastnum", calnum.toString());
		$ctx.dataBind();

	}

	function analsisByCode(sender, args) {
		var vbillcode = $ctx.getString("vbillcode");
		//$ctx.put("code", );
		if (NCMMPubUtil.isEmpty($ctx.getString("vcode"))) {
			return;
		}
		analysisMtrlInfoShow($ctx.getString("vcode"));
		$id("textbox3").set("value", null);
		$id("textbox3").focus();
	
	}


// 打开参照页面
	function openRefPage(ncbillcode, ctxnamekey,ctxpkkey,pkfiel, pk_stordoc) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;
		});

		if(jsonparam == null) {
			//$alert("jsonparam is null!");
			return;
		}

		jsonparam["wheresql"] = " where pk_stordoc='" + pk_stordoc + "'";
        console.log(jsonparam["wheresql"]);
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["name"]);
				$ctx.put(ctxpkkey, refdata[pkfiel]);
				$ctx.dataBind();
				$cache.write("default" + ctxnamekey, $jsonToString(refdata));

				//设置了默认库存组织后，自动下载默认库存组织下的仓库
				//where pk_org='库存组织编码'
				if(ctxnamekey == "stockorg") {
					var condition = {
						"pk_org": "='" + refdata["code"] + "'"
					};
					processDocDataFromNC(StordocVO, condition);
				}
			}
		});
	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
				}
				return;
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}
function com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackRef(sender, args){
     var stordoc = $cache.read("defaultstordoc");
        if(stordoc == null || stordoc == "") {
            $alert("默认仓库不能为空，请设置！");
            return;
        }
        var pk_stordoc = $stringToJSON(stordoc).pk_stordoc;
        openRefPage("PDARACK", "pk_rackname", "pk_rack", "pk_rack", pk_stordoc);
}
function com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackEdit(sender, args){
     var rack = $ctx.getString("pk_rackname");
		if(NCMMPubUtil.isEmpty(rack)) {
			$ctx.put("pk_rackname", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		var wheresql = " where (code='" + rack + "' or name ='" + rack + "')";
		var stockID = NCMMPUINUtil.getDefaultStordocPK();
		if(!NCMMPubUtil.isEmpty(stockID)) {
			wheresql = wheresql + " and pk_stordoc='" + stockID + "'";
		}
		getRackInfoByWhereSql(wheresql, rack);
}
function com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackScan(sender, args){
      $scanner.open({
			bindfield: "rack",
			callback: "rackScanBack()"
		});
	}

	function rackScanBack() {
		var rack = $ctx.getString("rack");
		var wheresql = " where code='" + rack + "'";
		var stockID = NCMMPUINUtil.getDefaultStordocPK();
		if(!NCMMPubUtil.isEmpty(stockID)) {
			wheresql = wheresql + " and pk_stordoc='" + stockID + "'";
		}
		getRackInfoByWhereSql(wheresql, rack);
	}

	function getRackInfoByWhereSql(wheresql, rack) {
		if(NCMMPubUtil.isTableExist(RackVO) == false) {
			$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于默认仓库！");
			$ctx.put("pk_rackname", "");
			$ctx.put("pk_rack", "");
			$ctx.dataBind();
			return;
		}
		NCMMPubUtil.queryVO(RackVO, wheresql, function(data) {
			//2 打开扫描记录编辑界面
			if(NCMMPubUtil.isEmpty(data)) {
				$alert("没有扫描货位【" + rack + "】记录，请更新货位数据，或检查当前扫描货位是否属于默认仓库！");
				$ctx.put("pk_rackname", "");
				$ctx.put("pk_rack", "");
				$ctx.dataBind();
				return;
			}
			var vos = eval(data);
			$ctx.put("pk_rackname", vos[0].name);
			$ctx.put("pk_rack", vos[0].pk_rack);
			$ctx.dataBind();
		});
	}	com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController.prototype = {
    rackScan : com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackScan,
    rackEdit : com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackEdit,
    rackRef : com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$rackRef,

		initialize : com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$initialize,
		evaljs : com$yonyou$ncmm_pac_pda$PurchaseInScanEditPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController.registerClass('com.yonyou.ncmm_pac_pda.PurchaseInScanEditPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}